#!/bin/bash
# Copyright 2014, Nathan Milford
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
lib_dir='/var/lib/etcd/'
# Will be used to populate IP address values below.  Setting some items to
# '0.0.0.0' is not compatable with the discovery API.
#_MY_IPADDR=$(/sbin/ifconfig | grep 'inet'| grep -Ev '(127|117|172|::1|fe)' |awk '{print $2}'|head -n 1)
_MY_IPADDR=192.168.3.56
# Daemon User
#ETCD_USER="etcd"
 
# Cluster Seeds
#  You can specify a list here sepearated by commas, or leave it blank if
#  you're playing with a single node.
ETCD_SEEDS=""

ETCD_INITIAL_CLUSTER="java56=http://192.168.3.56:7001,java55=http://192.168.3.55:7001,java60=http://192.168.3.60:7001"
 
# Discovery Endpoint
#  Leave it as the public URL unless you are running your own.
# ETCD_DISCOVER_ENDPOINT="https://discovery.etcd.io/"
 
# Discovery Token
#  If you are using the discovery protocol you can grab your cluster token
#  from https://discovery.etcd.io/new if you are not hosting it yourself.
# ETCD_DISCOVERY_TOKEN="cb5940a807d44287e05f29f3170883e3"
 
# This node's name as it represents itself on the cluster.
ETCD_NODE_NAME=$(hostname -s)
 
# Hostname and port for the etcd server to work on.
ETCD_LISTEN="http://$_MY_IPADDR:4001,http://127.0.0.1:4001"

ADVERTISE_CLIENT_URLS="http://$_MY_IPADDR:4001"
# Directory to store log and snapshot.
ETCD_DATA_DIR="/var/lib/etcd/"
 
# File to log stdout/stderr to.
ETCD_OUT_FILE="/var/log/etcd/etcd.log"
 
# Set logging vebosity for the file above.
#   Valid options are "", "v" or "vv"
ETCD_LOGGING=""
 
 
# Set security settings for the etcd server.
#  Leave blank if you do not plan to use this feature, otherwise add appropriate
#  paths.
ETCD_CAFILE=""
ETCD_CERT=""
ETCD_KEY=""
 
# Toggles snapshotting.
#  Keep blank or set to true.
ETCD_SNAPSHOT=""
 
# Hostname and port for the RAFT server to work on.
RAFT_LISTEN="http://$_MY_IPADDR:7001"

INITIAL_ADVERTISE_PEER_URLS="http://$_MY_IPADDR:7001"
# Set security settings for the RAFT server.
#  Leave blank if you do not plan to use this feature, otherwise add appropriate
#  paths.
RAFT_CAFILE=""
RAFT_CERT=""
RAFT_KEY=""
 
# Below we build the opts to pass to the init script.
 
ETCD_OPTS="-name=${ETCD_NODE_NAME} \
          --listen-client-urls=${ETCD_LISTEN} \
	  --advertise-client-urls=${ADVERTISE_CLIENT_URLS} \
          --listen-peer-urls=${RAFT_LISTEN} \
	  --initial-advertise-peer-urls=${INITIAL_ADVERTISE_PEER_URLS} \
          -data-dir=${ETCD_DATA_DIR}"

if [ x$ETCD_INITIAL_CLUSTER != "x" ]; then
  ETCD_OPTS="$ETCD_OPTS --initial-cluster=${ETCD_INITIAL_CLUSTER}"
fi
 
if [ x$ETCD_SEEDS != "x" ]; then
  ETCD_OPTS="$ETCD_OPTS -peers=${ETCD_SEEDS}"
fi
 
if [ x$ETCD_DISCOVERY_TOKEN != "x" ]; then
  ETCD_OPTS="$ETCD_OPTS -discovery=${ETCD_DISCOVER_ENDPOINT}${ETCD_DISCOVERY_TOKEN}"
fi
 
if [ "$ETCD_LOGGING" == "v" ]; then
  ETCD_OPTS="$ETCD_OPTS -v"
elif [ "$ETCD_LOGGING" == "vv" ]; then
  ETCD_OPTS="$ETCD_OPTS -vv"
fi
 
if [ x$ETCD_SNAPSHOT != "x" ]; then
  ETCD_OPTS="$ETCD_OPTS -snapshot"
fi
if [ ! -d $lib_dir ];then
  mkdir -p $lib_dir
fi
#if [ x$ETCD_CAFILE != "x" ] && [ x$ETCD_CERT != "x" ] && [ x$ETCD_KEY != "x" ]; then
#  ETCD_OPTS="$ETCD_OPTS -ca-file=${ETCD_CAFILE} -cert-file=${ETCD_CERT} -key-file=${ETCD_KEY}"
#fi
 
#if [ x$RAFT_CAFILE != "x" ] && [ x$RAFT_CERT != "x" ] && [ x$RAFT_KEY != "x" ]; then
#  ETCD_OPTS="$ETCD_OPTS -peer-ca-file=${RAFT_CAFILE} -peer-cert-file=${RAFT_CERT} -peer-key-file=${RAFT_KEY}"
#fi
# echo $ETCD_OPTS
# TODO
# Add support for:
#  -peers-file
#  -config
#  -cors
#  -cpuprofile
export ETCD_OPTS
